﻿using System.Diagnostics;
using PipeServer.FileUp.Model;

using RpcHelper;
using RpcHelper.Config;

namespace PipeServer.Config
{
        public class PipeConfig
        {
                private static readonly int _SyncTimeOut = LocalConfig.Local.GetValue ("pipe:Service:SyncTimeOut", 12000);
                private static string _DefaultPipeName = null;

                static PipeConfig ()
                {
                        string def = LocalConfig.Local.GetValue ("pipe:Service:PipeName",string.Empty);
                        if (def == string.Empty)
                        {
                                using (Process process = Process.GetCurrentProcess ())
                                {
                                        def = process.ProcessName;
                                }
                        }
                        _DefaultPipeName = def;
                }
                /// <summary>
                /// 默认管道名
                /// </summary>
                public static string DefaultPipeName
                {
                        get => _DefaultPipeName;
                        set => _DefaultPipeName = value;
                }
                private static Interface.IAllot _DefaultAllot = null;

                /// <summary>
                /// 默认的处理程序
                /// </summary>
                public static Interface.IAllot DefaultAllot
                {
                        get => PipeConfig._DefaultAllot;
                        set => PipeConfig._DefaultAllot = value;
                }

                private static Interface.IPipeEvent _SocketEvent = null;

                /// <summary>
                /// Pipe事件
                /// </summary>
                public static Interface.IPipeEvent PipeEvent
                {
                        get => PipeConfig._SocketEvent;
                        set => PipeConfig._SocketEvent = value;
                }
                private static string _ServerKey = LocalConfig.Local.GetValue ("pipe:Service:ServerKey", "6xy3#7a%ad").GetMd5 ();

                /// <summary>
                /// 服务器的链接密钥
                /// </summary>
                public static string ServerKey
                {
                        get => PipeConfig._ServerKey;
                        set
                        {
                                if (string.IsNullOrEmpty (value))
                                {
                                        return;
                                }
                                PipeConfig._ServerKey = Tools.GetMD5 (value);
                        }
                }


                /// <summary>
                /// 同步超时时间
                /// </summary>
                public static int SyncTimeOut => _SyncTimeOut;
                /// <summary>
                /// 超时时间
                /// </summary>
                public static int TimeOut { get; private set; } = LocalConfig.Local.GetValue ("pipe:Service:TimeOut", 10);

                /// <summary>
                /// 块大小(5MB)
                /// </summary>
                public static FileUpConfig UpConfig
                {
                        get;
                        set;
                } = LocalConfig.Local.GetValue ("pipe:Service:UpConfig", new FileUpConfig());
        }
}
